package com.chen.lifepic.LargeViewFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

import com.chen.lifepic.LifePic;
import com.chen.lifepic.R;
import com.chen.lifepic.DataProvider.RemoteGrideViewProvider;
import com.chen.lifepic.thumbnailManager.ThumbnailManager;
import com.chen.lifepic.utils.Utils;

public class SlideShowFragment extends Fragment implements OnClickListener,
		OnTouchListener {
	float lastX = 0;

	ViewGroup _slideShowRoot = null;

	LargeSubView _slideShowImageView = null;
	
	LargeView _lareView = null;

	Button _buttonLike = null;

	String _url = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// PicManager.getInstance().registerImageReadyListener(this);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// PicManager.getInstance().unregisterImageReadyListener(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		_slideShowRoot = (ViewGroup) inflater.inflate(
			R.layout.large_view_fragment, container, false);
		
		_slideShowImageView = new LargeSubView(this.getActivity());
		_slideShowImageView.setLayoutParams(new LayoutParams(
			LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		_slideShowRoot.addView(_slideShowImageView);
		_slideShowImageView.setOnTouchListener(this);

		_slideShowRoot.addView(_lareView);
		_buttonLike = (Button) _slideShowRoot.findViewById(R.id.button_like);
		if (null != _buttonLike) {
			_buttonLike.setOnClickListener(this);
		}
		return _slideShowRoot;
	}

	@Override
	public void onResume() {
		super.onResume();
		ThumbnailManager.getInstance().removeOldTasksAndReadyForNewTasks();
		RemoteGrideViewProvider provider = ((LifePic) getActivity()).getRemoteGrideViewProvider();
		if (null == provider)
			return;

		int index = provider.getSelectedIndex();

		_slideShowImageView.setMediaInfo(provider.getItem(index));
	}

	@Override
	public void onClick(View v) {
		if (null != _buttonLike && v == _buttonLike) {
			// _buttonLike.setBackground();
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		float x = 0;
		if (MotionEvent.ACTION_DOWN == event.getAction()) {

		} else if (MotionEvent.ACTION_MOVE == event.getAction()) {
			x = lastX - event.getX();
		}
		// float y = event.getY();
		Utils.logDebug("test", "offset = " + x);
		v.scrollBy((int) x, (int) v.getTop());
		lastX = event.getX();
		return true;
	}

}
